//
//  LCWeclomeViewVc.swift
//  SinaWeiBo
//
//  Created by mac on 16/12/5.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import SDWebImage

class LCWeclomeViewVc: UIViewController {
    
    var model: LCOAuthModel?
    //懒加载控件
    //背景图片
    lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    lazy var iconImageView: UIImageView = {
       let icon = UIImageView()
        if let iconUrl = LCOAuthViewModel.sharedViewModel.userAccount?.avatar_large{
            icon.sd_setImage(with: URL(string: iconUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        icon.layer.cornerRadius = 45
        icon.layer.masksToBounds = true
        return icon
    }()
    //信息
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        if let nameLabel = LCOAuthViewModel.sharedViewModel.userAccount?.name{
            label.text = "欢迎回来\(nameLabel)"
        }else{
            label.text = "欢迎回来"
        }
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 13)
        
        label.alpha = 0
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    //设置UI
    func setupUI(){
        //添加子控件
        view.addSubview(backImageView)
        view.addSubview(iconImageView)
        view.addSubview(messageLabel)
        //设置约束
        backImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view).offset(300)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
        }
        
    }
    //在视图已经显示的时候调用动画方法，因为在别的方法中调用动画方法，动画可能播放不完全
    override func viewDidAppear(_ animated: Bool) {
        startAnimation()
    }
    
    //开始动画的方法
    private func startAnimation(){
        //1更新约束
        iconImageView.snp_updateConstraints { (make) in
            make.top.equalTo(self.view).offset(100)
        }
        //2添加动画
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: UIViewAnimationOptions(rawValue: 0), animations: {() -> Void in
            //需要添加动画的视图
           self.view.layoutIfNeeded()
           
        
        }, completion: {(Bool) -> Void in
            
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1
            }, completion: { (isSuccess) in
                //动画结束后的操作
                //发送通知
                NotificationCenter.default.post(name: NSNotification.Name(notificationName), object: nil)
                
            })
            
        })
        
        
    }


}
